package xxh;

import chj.Bookmanage.Bfind;
import ldz.Show.LoginWindow;
import nz.UserProfileUI;

import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;

public class MainWindow extends JFrame {
    private JPanel mainPanel;  // 用于显示 BorrowBook 窗口
    private JPanel toolbar;
    private JButton btnBorrowBook;
    private JButton btnReturnBook;
    private JButton btnSelectBook;
    private JButton btnBackLogin;
    private JButton btnborrowRecord;
    private JButton btnUserInfo;

    public MainWindow(String Uno) {
        // 设置窗口属性
        setTitle("图书管理借阅子界面");
        setSize(800, 600);
        setResizable(false);
        setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        // 使用BorderLayout布局管理器0
        setLayout(new BorderLayout());

        // 创建并设置背景面板
        mainPanel = new BackgroundPanel("src/xxh/img/bk.jpg", 800, 600);  // 使用背景图片，设置大小

        // 创建工具栏（按钮）
        toolbar = new JPanel();
        toolbar.setLayout(new FlowLayout(FlowLayout.CENTER));

        // 创建按钮并添加事件监听
        btnBorrowBook = new JButton("借阅书籍");
        btnReturnBook = new JButton("归还书籍");
        btnSelectBook = new JButton("查询书籍");
        btnBackLogin = new JButton("退出登录");
        btnborrowRecord = new JButton("查询借阅记录");
        btnUserInfo = new JButton("个人信息");

        btnBorrowBook.addActionListener(e -> {
            try {
                // 创建 BorrowBook 窗口实例
                BorrowUI borrowBookWindow = new BorrowUI(Uno);
                borrowBookWindow.setOpaque(false);
                // 在 mainPanel 中显示 BorrowBook 窗口
                mainPanel.removeAll();
                mainPanel.add(borrowBookWindow, BorderLayout.CENTER);
                mainPanel.revalidate(); // 刷新显示
                mainPanel.repaint(); // 重绘面板
            } catch (SQLException | ClassNotFoundException ex) {
                throw new RuntimeException(ex);
            }
        });

        btnReturnBook.addActionListener(e -> {
            try {
                // 创建 ReturnBook 窗口实例
                ReturnUI returnBookWindow = new ReturnUI(Uno);
                // 在 mainPanel 中显示 ReturnBook 窗口
                mainPanel.removeAll();
                mainPanel.add(returnBookWindow, BorderLayout.CENTER);
                mainPanel.revalidate(); // 刷新显示
                mainPanel.repaint(); // 重绘面板
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        });

        btnSelectBook.addActionListener(e -> {
            Bfind.BookLookFram();
        });

        btnBackLogin.addActionListener(e -> {
            new LoginWindow().setLocationRelativeTo(null);
            dispose();
        });

        btnborrowRecord.addActionListener(e -> {
            BorrowRecordUI borrowRecordUI = new BorrowRecordUI(Uno);
            borrowRecordUI.setLocationRelativeTo(null);
            borrowRecordUI.setVisible(true);
        });

        btnUserInfo.addActionListener(e -> {
            mainPanel.removeAll();
            mainPanel.add(new UserProfileUI(Uno), BorderLayout.CENTER);
            mainPanel.revalidate(); // 刷新显示
            mainPanel.repaint(); // 重绘面板
        });

        // 将按钮添加到工具栏
        toolbar.add(btnBorrowBook);
        toolbar.add(btnReturnBook);
        toolbar.add(btnSelectBook);
        toolbar.add(btnborrowRecord);
        toolbar.add(btnUserInfo);
        toolbar.add(btnBackLogin);

        // 将工具栏放在顶部
        add(toolbar, BorderLayout.NORTH);

        // 创建主面板并添加到窗口的中央区域
        add(mainPanel, BorderLayout.CENTER);
    }
}
